
/**
 * Copyright 2008,2009 Oleg Frenkel
 *
 * This file is part of PerfectSearch.
 *
 * PerfectSearch is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * PerfectSearch is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PerfectSearch.  If not, see <http://www.gnu.org/licenses/>.
 */

package oleg.util;

public class StringUtils
{
  static public String[] split(String str, String splitBy) {
    // Find out length
    int occurence = 0;
    for (int curPos = 0; 
         (curPos = str.indexOf(splitBy, curPos)) >= 0; 
         curPos += splitBy.length(), occurence++)
      ;

    String[] res = new String[occurence + 1];

    int i = 0;
    int prevPos = 0;
    int curPos = 0; 
    while (true) {
      curPos = str.indexOf(splitBy, curPos); 
      if (curPos == -1) {
        res[i++] = str.substring(prevPos);
        break;
      }

      res[i++] = str.substring(prevPos, curPos);
      curPos += splitBy.length();
      prevPos = curPos;
    }
    
    return res;
  }
}